//
//  ViewController.m
//  14-UICollectionView
//
//  Created by 曹曹 on 16/8/2.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "ViewController.h"

#define kCollectionViewCellReuse @"collectionCellReuse"
#define kCollectionViewHeaderView @"header"
#define kCollectionViewFooterView @"footer"

@interface ViewController () <UICollectionViewDelegate, UICollectionViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 1.itemSize, 默认大小(50, 50)
    layout.itemSize = CGSizeMake(CGRectGetWidth(self.view.bounds) / 2 - 7.5, 100);
    // 2.scrollDirection 默认水平滚动
    // UICollectionViewScrollDirectionVertical水平滚动
    // UICollectionViewScrollDirectionHorizontal竖直滚动
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    // 滚动方向的改变会改变行和列,包括区
    // 3.minimumLineSpacing最小行间距
    // 4.minimumInteritemSpacing最小列间距
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing = 5;
    // 5.sectionInset
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    // 6.headerReferenceSize区头size
    // 水平滚动时设置宽度
    // 竖直滚动时设置高度
    layout.headerReferenceSize = CGSizeMake(100, 100);
    // 7.footerReferenceSize区尾size
    layout.footerReferenceSize = CGSizeMake(200, 200);
    // 8.区头悬浮效果 iOS 9以后
    layout.sectionHeadersPinToVisibleBounds = YES;
    layout.sectionFootersPinToVisibleBounds = YES;
    
    // UICollectionView
    // 参数1.frame
    // 参数2.填入不能为空的layout(布局)
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    [self.view addSubview:collectionView];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    // cell
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewCellReuse];
    // 区头
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeaderView];
    // 区尾
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kCollectionViewFooterView];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 30;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellReuse forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor redColor];
    
    return cell;
}
#warning - headerView and footerView
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeaderView forIndexPath:indexPath];
        reusableView.backgroundColor = [UIColor yellowColor];
        return reusableView;
    } else {
        UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kCollectionViewFooterView forIndexPath:indexPath];
        reusableView.backgroundColor = [UIColor greenColor];
        return reusableView;

    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
